/*
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.properties.bean;

import com.hyts.properties.base.PropertyBean;
import com.hyts.properties.config.PropertyConfiguration;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.Objects;
import java.util.Properties;



/**
 * @author LiBo/Alex
 * @version V1.0
 */
public class SinglePropertyBean extends PropertyBean<Properties,String> {

    /**
     * @constructor
     * SPropertyBean
     */
    public SinglePropertyBean(String configPath, String configName) {
        setConfigPath(configPath);
        setConfigName(configName);
        setModel(new Properties());
    }

    /**
     */
    public SinglePropertyBean(String configName) {
        setConfigName(configName);
        setConfigPath(PropertyConfiguration.DEFAULT_PROPERTIES_FILE_PATH);
        setModel(new Properties());
    }

    /**
     */
    public SinglePropertyBean() {
        setConfigPath(PropertyConfiguration.DEFAULT_PROPERTIES_FILE_PATH);
        setConfigName(PropertyConfiguration.DEFAULT_PROPERTIES_FILE_NAME);
        setModel(new Properties());
    }

    /*
     * @return
     * @see com.hyts.property.base.PropertyBean#load()
     * @exception
     */

    @Override
    public SinglePropertyBean load() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = classLoader.getResource(MessageFormat.format("{0}{1}",getConfigPath(),getConfigName()));
        File file = null;
        try {
            if(Objects.isNull(url)){
                return new SinglePropertyBean();
            }
            file = new File(URLDecoder.decode(url.getPath(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if(file.exists()) {
            if(file.canRead()) {
                if(file.lastModified() > getLastModifiedTime()) {
                    try(
                        InputStream is = classLoader.getResourceAsStream(MessageFormat.format("{0}{1}",getConfigPath(),getConfigName()));
                            ) {
                        getModel().load(is);
                        setLastModifiedTime(file.lastModified());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else {
                    return this;
                }
            }
        }else {
            try {
                throw new FileNotFoundException(MessageFormat.format("参数详情如下:{0}{1}{2}",getConfigPath(),File.separator,getConfigName()));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return this;
    }

    /*
     * @see com.hyts.property.base.PropertyBean#reload()
     * @exception
     */
    @Override
    public SinglePropertyBean write() {
        if(getModel() == null)
            throw new RuntimeException("参数模型传递为空！");
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = classLoader.getResource(MessageFormat.format("{0}{1}",getConfigPath(),getConfigName()));
        File file = new File(url.getPath());
        try(
            FileOutputStream fos = new FileOutputStream(file);
            ) {
            getModel().store(fos, "存储更新配置文件");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    /*
     * @param key
     * @return
     * @see com.hyts.property.base.PropertyBean#get(java.lang.String)
     * @exception
     */
    public Object get(String key) {
        return getModel().get(key);
    }

    /*
     * @param key
     * @param value
     * @return
     * @see com.hyts.property.base.PropertyBean#set(java.lang.String, java.lang.Object)
     * @exception
     */
    public boolean set(String key,Object value) {
        synchronized(this) {
            return getModel().put(key, value) != value;
        }
    }
}
